/***********************************************
 * Title:       TermManageAction.java
 * Description: TermManageAction.java
 * Author:      stu
 * Create Date: 2010-3-23
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.work;

import org.infohazard.maverick.flow.ControllerContext;

import com.younker.waf.maverick.DispatchCtl;

import edu.tju.runtimeConfig.AdmissionStateService;
import edu.tju.runtimeConfig.CourseScheduleService;
import edu.tju.runtimeConfig.TermService;

/**
 * @author stu
 *
 */
public class TermManageAction extends DispatchCtl
{
        private final static String V_TERM = "term";
        
        private int year1;
        private int year2;
        private int term;

        /**
         * @return the year1
         */
        public int getYear1()
        {
                return year1;
        }

        /**
         * @param year1 the year1 to set
         */
        public void setYear1(int year1)
        {
                this.year1 = year1;
        }

        /**
         * @return the year2
         */
        public int getYear2()
        {
                return year2;
        }

        /**
         * @param year2 the year2 to set
         */
        public void setYear2(int year2)
        {
                this.year2 = year2;
        }

        /**
         * @return the term
         */
        public int getTerm()
        {
                return term;
        }

        /**
         * @param term the term to set
         */
        public void setTerm(int term)
        {
                this.term = term;
        }

        /* (non-Javadoc)
         * @see org.infohazard.maverick.ctl.FormBeanUser#makeFormBean(org.infohazard.maverick.flow.ControllerContext)
         */
        @Override
        protected Object makeFormBean(ControllerContext arg0)
        {
                return new TermManageAction();
        }
        
        public String nextTerm( Object beans, ControllerContext ctx )
        {
                String newTerm = TermService.getInstance().nextTerm();
                clearRuntimeConfig( newTerm );
                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                return V_TERM;
        }
        
        public String changeTerm( Object beans, ControllerContext ctx )
        {
                TermManageAction bean = (TermManageAction)beans;
                int year1 = bean.getYear1();
                int year2 = year1 + 1;
                int term = bean.getTerm();
                StringBuffer sb = new StringBuffer();
                sb.append(year1);
                sb.append(year2);
                sb.append(term);
                TermService.getInstance().updateTerm(sb.toString());
                clearRuntimeConfig(sb.toString());
                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                return V_TERM;
        }
        
        private void clearRuntimeConfig( String newTerm )
        {
                AdmissionStateService.getInstance().clearValue(newTerm);
                CourseScheduleService.getInstance().clearValue(newTerm);
        }
        
        

}
